﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tester.Views;
using Newtonsoft.Json;

namespace Tester.Models;
internal class ReadAndWrite
{
    private static async void Read()
    {
#if ANDROID
        PermissionStatus statusread = await Permissions.RequestAsync<Permissions.StorageRead>();
        string path = "/storage/emulated/0/Tester";
        if (File.Exists(path + "/test.json") == false)
        {
            CreateNew();
            Read();
        }

#elif WINDOWS
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Tester\\test.json";
        if (File.Exists(path) == false)
        {
            CreateNew();
            Read();
        }
#endif
    }

    private static async void Write(object ObjectToWrite)
    {
#if ANDROID
        PermissionStatus statuswrite = await Permissions.RequestAsync<Permissions.StorageWrite>();
        string path = "/storage/emulated/0/Tester";
        if (Directory.Exists(path) == false)
        {
            Directory.CreateDirectory(path);

        }
        FileStream fs = new(path + "/test.json", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
        string str = JsonConvert.SerializeObject(ObjectToWrite);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#elif WINDOWS
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Tester";
        if (Directory.Exists(path) == false)
        {
            Directory.CreateDirectory(path);

        }
        FileStream fs = new(path + "\\test.json", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
        string str = JsonConvert.SerializeObject(ObjectToWrite);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#endif
    }

    private static async void CreateNew()
    {

        /* 项目“Tester (net6.0-ios)”的未合并的更改
        在此之前:
                using Views.CommonConfig config = new();
        在此之后:
                using Tester.Views.CommonConfig config = new();
        */

        /* 项目“Tester (net6.0-maccatalyst)”的未合并的更改
        在此之前:
                using Views.CommonConfig config = new();
        在此之后:
                using Tester.Views.CommonConfig config = new();
        */

        /* 项目“Tester (net6.0-windows10.0.19041.0)”的未合并的更改
        在此之前:
                using Views.CommonConfig config = new();
        在此之后:
                using Tester.Views.CommonConfig config = new();
        */
        using CommonConfig config = new();
        config.themes.Add("跟随系统");
        config.themes.Add("浅色");
        config.themes.Add("深色");
        string? str = JsonConvert.SerializeObject(config);
#if WINDOWS
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Tester");
        FileStream fs = new(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Tester" + "\\test.json", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#elif ANDROID
        string path = "/storage/emulated/0/Tester";
        FileStream fs = new(path + "/test.json", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
        await sw.WriteAsync(str);
        sw.Close();
        fs.Close();
#endif
    }
}
